﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineAuction.Business;
using OnlineAuction.Entity;

namespace OnlineAuction
{
    public partial class Home1 : System.Web.UI.Page
    {
        private List<AuctionDTO> list = new List<AuctionDTO>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (Request.QueryString["ItemName"] == null && Request.QueryString["CategoryID"] == null)
                {
                    AuctionBO abo = new AuctionBO();
                    list = abo.ViewAuction();
                }
                else
                {
                    ItemBO bo = new ItemBO();
                    list = bo.Search(Request.QueryString["ItemName"].ToString(),
                                    int.Parse(Request.QueryString["CategoryID"].ToString()));
                }
                if (list != null)
                {
                    Cp.PageSize = 3;
                    Cp.ShowFirstLast = true;
                    Cp.DataSource = list;
                    Cp.BindToControl = dlAuction;
                    dlAuction.DataSource = Cp.DataSourcePaged;
                }
            }

        }
        public string CountDownScript()
        {
            string bscr = "";
            int n = dlAuction.Items.Count;

            for (int i = 0; i < n; i++)
            {
                Label days = (Label)dlAuction.Items[i].FindControl("lblDays");
                Label hours = (Label)dlAuction.Items[i].FindControl("lblHours");
                Label minutes = (Label)dlAuction.Items[i].FindControl("lblMinutes");
                Label seconds = (Label)dlAuction.Items[i].FindControl("lblSeconds");
                Literal lid = (Literal)dlAuction.Items[i].FindControl("lblAuID");
                string aid = lid.Text;
                bscr += "<script type=\"text/javascript\">" +
                     "var days" + aid + " = " + days.Text + ";" +
                       "var hours" + aid + " =" + hours.Text + ";" +
                       "var minutes" + aid + " =" + minutes.Text + ";" +
                       "var seconds" + aid + " = " + seconds.Text + ";" +
                       "CounterTimer" + aid + "();" +
                       "var interv=setInterval(CounterTimer" + aid + ",1000);" +
                       "function CounterTimer" + aid + "()" +
                       "{" +
                       "document.getElementById(\"lblDays" + aid + "\").innerHTML=days" + aid + ";" +
                       "document.getElementById(\"lblHours" + aid + "\").innerHTML=hours" + aid + ";" +
                       "document.getElementById(\"lblMinutes" + aid + "\").innerHTML=minutes" + aid + ";" +
                       "document.getElementById(\"lblSeconds" + aid + "\").innerHTML=seconds" + aid + ";" +
            "seconds" + aid + "--;" +
            "if (seconds" + aid + " <= 0) {" +
               " if (minutes" + aid + " > 0) {" +
                    "minutes" + aid + "--;" +
                    "seconds" + aid + " = 59;" +
                "} else if (minutes" + aid + " <= 0) {" +
                    "if (hours" + aid + " > 0) {" +
                        "hours" + aid + "--;" +
                        "minutes" + aid + " = 59;" +
                        "seconds" + aid + " = 59;" +
                    "} else if (hours" + aid + " <= 0) {" +
                        "if (days" + aid + " > 0) {" +
                            "days" + aid + "--;" +
                            "hours" + aid + " = 23;" +
                            "minutes" + aid + " = 59;" +
                            "seconds" + aid + " = 59;" +
                        "}" +
                "else if (days" + aid + " <= 0) {" +
                 "document.getElementById(\"lblDays" + aid + "\").innerHTML=0;" +
                       "document.getElementById(\"lblHours" + aid + "\").innerHTML=0;" +
                       "document.getElementById(\"lblMinutes" + aid + "\").innerHTML=0;" +
                          "document.getElementById(\"lblSeconds" + aid + "\").innerHTML=0;" +
                          "clearInterval(interv);" +
               "} } } }" +
               "}" +
                       "</script>";
            }
            return bscr;
        }
    }
}